package cn.alibab.cangku.service.impl;


import cn.alibab.cangku.dao.ProductDao;
import cn.alibab.cangku.model.BizProductCategory;
import cn.alibab.cangku.service.ProductCategoryService;
import cn.alibab.cangku.vo.CategoryVo;
import cn.alibab.common.model.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    private final ProductDao productDao;

    public ProductCategoryServiceImpl(ProductDao productDao) {
        this.productDao = productDao;
    }
//查询库存分类的一级分类
    @Override
    public R findAllProductCategory() {
        List<BizProductCategory> allProductCategory = productDao.findAllProductCategory();
        return R.ok(allProductCategory);
    }

    @Override
    public R findAllProductTwoCategory(Long pid) {
        List<BizProductCategory> allProductTwoCategory = productDao.findAllProductTwoCategory(pid);
        return R.ok(allProductTwoCategory);
    }

    @Override
    public R getCategory() {
        List<BizProductCategory> category = productDao.getCategory();
        List<CategoryVo> categoryVoList = new ArrayList<>();
        createCategory(category,categoryVoList,0L);
        return R.ok(categoryVoList);
    }
    public void createCategory(List<BizProductCategory> category,List<CategoryVo> categoryVoList,Long pid){
        category.forEach(item->{
            if (item.getPid().equals(pid)){
                CategoryVo categoryVo = new CategoryVo();
                BeanUtils.copyProperties(item,categoryVo);
                categoryVo.setChildren(new ArrayList<>());
                createCategory(category,categoryVo.getChildren(),item.getId());
                categoryVoList.add(categoryVo);
            }
        });


    }


}
